package com.viphuli.app.tool.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import butterknife.Bind;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.JsonUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.CircleHospital;
import com.viphuli.app.tool.common.Constants;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;
import com.viphuli.app.tool.http.ApiRequest;

public class CircleHospitalListFragment extends BaseProgressFragment implements OnItemClickListener {

	@Bind(R.id.id_listview)
	protected ListView listview;

	private BaseAdapter adapter;

	List<CircleHospital> list = new ArrayList<CircleHospital>();

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_circle_hospital_list;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

		listview.setOnItemClickListener(this);

	}

	@Override
	protected void initData() {

		initRequest(HandlerType.waitting);

	}

	@Override
	protected void initRequest(HandlerType handlerType) {
		super.initRequest(handlerType);

		RequestParams params = new RequestParams();
		params.put("address_code", 1000);//北京

		ApiRequest.circleHospitalList.request(this, params, handlerType, false);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (R.id.id_menu_hospital_list == item.getItemId()) {
			CircleHospitalAddFragment.go(getActivity());
			return true;
		}

		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getActivity().getMenuInflater().inflate(R.menu.menu_circle_hospital_list, menu);

		return true;
	}

	public ListView getListview() {
		return listview;
	}

	public List<CircleHospital> getList() {
		return list;
	}

	public void setAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}

	public static void go(Activity act) {

		Bundle params = new Bundle();
		MyPageHelper.circleHospitalList.showMyPage(act, params, Constants.ACTIVITY_REQUEST_HOSPITAL_SELECT_CODE);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

		CircleHospital hospital = list.get(position);
		setResultAndFinish(hospital);

	}

	/**
	 * 设置结果并结束
	 * @param localName
	 * @param address
	 */
	private void setResultAndFinish(CircleHospital hospital) {

		Intent intent = new Intent();
		intent.putExtra("circle_hospital", hospital != null ? JsonUtils.toJson(hospital) : null);
		caller.setResult(Activity.RESULT_OK, intent);
		caller.finish();

	}
}
